package com.community.contents.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.community.contents.entity.TopicUserEntity;
import com.community.contents.mapper.TopicUserMapper;
import com.community.contents.service.ITopicUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 用户关注的圈子表 服务实现类
 * </p>
 *
 * @author hjs
 * @since 2023-02-28
 */
@Service
public class TopicUserServiceImpl extends ServiceImpl<TopicUserMapper, TopicUserEntity> implements ITopicUserService {
    /**
     * 判断登录用户是否加入了圈子
     * @param topicId 圈子id
     * @param userId  用户id
     * @return
     */
    @Override
    public boolean isJoin(Integer topicId, Integer userId) {

        QueryWrapper<TopicUserEntity> queryWrapper = new QueryWrapper();
        queryWrapper.eq("topic_id",topicId);
        queryWrapper.eq("user_id",userId);
        TopicUserEntity topicUserEntity = baseMapper.selectOne(queryWrapper);
        return topicUserEntity!=null;

    }

    /**
     * 用户加入圈子
     * @param topicId 圈子id
     * @param userId 用户id
     * @return
     */
    @Override
    public boolean joinTopic(int topicId, Integer userId) {
        TopicUserEntity topicUserEntity = new TopicUserEntity();
        topicUserEntity.setTopicId(topicId);
        topicUserEntity.setUserId(userId);
        topicUserEntity.setCreateTime(new Date());
        return baseMapper.insert(topicUserEntity)>0;
    }
}
